package com.lhh.adminlte.service.impl;

import com.lhh.adminlte.dao.MemberMapper;
import com.lhh.adminlte.entity.Member;
import com.lhh.adminlte.entity.MemberExample;
import com.lhh.adminlte.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/31 10:16
 * @Description:
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    MemberMapper memberMapper;

    @Override
    public List<Member> selectMembers(String phone, Integer status, Integer bonusMin, Integer bonusMax, Date createDateStart, Date createDateEnd, int displayStart, int displayLength) {
        MemberExample memberExample = new MemberExample();
        memberExample.setOffset(displayStart);
        memberExample.setLimit(displayLength);
        MemberExample.Criteria criteria = memberExample.createCriteria();
        if (!StringUtils.isEmpty(phone)) {
            criteria.andPhoneEqualTo(phone);
        }
        if (null != status && 0 != status) {
            criteria.andStatusEqualTo(status);
        }
        if (null != bonusMin) {
            criteria.andBonusGreaterThanOrEqualTo(bonusMin);
        }
        if (null != bonusMax) {
            criteria.andBonusLessThanOrEqualTo(bonusMax);
        }
        if (null != createDateStart) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createDateStart);
        }
        if (null != createDateEnd) {
            criteria.andCreateTimeLessThanOrEqualTo(createDateEnd);
        }
        return memberMapper.selectByExample(memberExample);
    }

    @Override
    public long countMembers(String phone, Integer status, Integer bonusMin, Integer bonusMax, Date createDateStart, Date createDateEnd) {
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        if (!StringUtils.isEmpty(phone)) {
            criteria.andPhoneEqualTo(phone);
        }
        if (null != status && 0 != status) {
            criteria.andStatusEqualTo(status);
        }
        if (null != bonusMin) {
            criteria.andBonusGreaterThanOrEqualTo(bonusMin);
        }
        if (null != bonusMax) {
            criteria.andBonusLessThanOrEqualTo(bonusMax);
        }
        if (null != createDateStart) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createDateStart);
        }
        if (null != createDateEnd) {
            criteria.andCreateTimeLessThanOrEqualTo(createDateEnd);
        }
        return memberMapper.countByExample(memberExample);
    }

    @Override
    public boolean updateMember(int id, int status) {
        Member member = memberMapper.selectByPrimaryKey(id);
        if (member != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            member.setStatus(status);
            member.setPs("更新时间:" + sdf.format(new Date()));
            memberMapper.updateByPrimaryKey(member);
            return true;
        }
        return false;
    }
}
